package BookManagement.dao;

import BookManagement.domain.Purchase;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository
public interface PurchaseDao {

    @Insert("insert into purchase values(#{bookISBN},#{userName},#{time},#{price},#{bookName},#{author})")
    public int purchase(Purchase purchase);

    @Select("<script>"+"select * from purchase"+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>"
    )
    public List<Purchase> findAllPurchase(HashMap<String,Object> map);

    @Select("<script>"+"select * from purchase where bookISBN=#{bookISBN}"+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>"
    )
    public List<Purchase> findAllPurchaseByBook(HashMap<String,Object>map);

    @Select("<script>"+"select * from purchase where username=#{username}"+
            "<if test='start!=null and size!=null'>"+"limit #{start},#{size}"+"</if>"
            +"</script>"
    )
    public List<Purchase> findAllPurchaseByUser(HashMap<String,Object> map);

    @Select("select count(*) from purchase where username=#{username}")
    public int findPurchaseCountByUser(String username);

    @Select("select count(*) from purchase where bookISBN=#{bookISBN}")
    public int findPurchaseCountByBook(String bookISBN);

    @Delete("delete from purchase where bookISBN=#{bookISBN} and username=#{username}")
    public int deletePurchase(String bookISBN,String username);
}
